package com.example.supernote.ui.note

import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun NoteRoute(
    noteViewModel: NoteViewModel,
    onBack: () -> Unit,
    snackbarHostState: SnackbarHostState = remember { SnackbarHostState() }
) {
    val coroutineScope = rememberCoroutineScope()
    val uiState by noteViewModel.uiState.collectAsStateWithLifecycle()

    NoteScreen(
        uiState = uiState,
        onBack = { onBack() },
        editContext = { noteViewModel.editContext(it) },
        editTitle = { noteViewModel.editTitle(it) },
        changeEditMode = { noteViewModel.changeEditMode() },
        execute = { command, context -> noteViewModel.execute(command, context) },
        apiByPrompt = { update, otherInfos ->
            coroutineScope.launch {
                noteViewModel.apiByPrompt(update, otherInfos)
            }
        },
        imageGenerateApiByPrompt = { update, otherInfos ->
            coroutineScope.launch {
                noteViewModel.imageGenerateApiByPrompt(update, otherInfos)
            }
        },
        imageOCRApi = { result, image, endLine ->
            coroutineScope.launch {
                noteViewModel.imageOCRApi(result, image, endLine)
            }
        },
        translateTextApi = { update, otherInfos ->
            coroutineScope.launch {
                noteViewModel.translateTextApi(update, otherInfos)
            }
        },
        audioApi = { updateTemplate, updateProgress, audioUri, context ->
            noteViewModel.audioApi(updateTemplate, updateProgress, audioUri, context)
        },
        saveFile = { coroutineScope.launch { noteViewModel.saveFile() } },
        changeOperation = { noteViewModel.changeOperation(it) },
        redo = { noteViewModel.redo() },
        undo = { noteViewModel.undo() },
        snackbarHostState = snackbarHostState
    )
}
